package report

import (
	"fmt"
	"time"

	"github.com/dustin/go-humanize"
	"github.com/shirou/gopsutil/v3/host"
	"gopkg.in/yaml.v2"
)

type HostInfo struct {
	Hostname             string `json:"hostname" yaml:"hostname"`
	Uptime               uint64 `json:"uptime" yaml:"uptime"`
	UptimeHuman          string `json:"uptimeHuman" yaml:"uptimeHuman"`
	BootTime             uint64 `json:"bootTime" yaml:"bootTime"`
	BootTimeHuman        string `json:"bootTimeHuman" yaml:"bootTimeHuman"`
	Procs                uint64 `json:"procs" yaml:"procs"`
	OS                   string `json:"os" yaml:"os"`
	Platform             string `json:"platform" yaml:"platform"`
	PlatformFamily       string `json:"platformFamily" yaml:"platformFamily"`
	PlatformVersion      string `json:"platformVersion" yaml:"platformVersion"`
	KernelVersion        string `json:"kernelVersion" yaml:"kernelVersion"`
	KernelArch           string `json:"kernelArch" yaml:"kernelArch"`
	VirtualizationSystem string `json:"virtualizationSystem" yaml:"virtualizationSystem"`
	VirtualizationRole   string `json:"virtualizationRole" yaml:"virtualizationRole"`
	HostID               string `json:"hostId" yaml:"hostId"`
}

func (o HostInfo) Show() {

	out, err := yaml.Marshal(o)
	if err != nil {
		return
	}

	fmt.Printf("os : %v\n", string(out))
}

func FetchOSInfo() HostInfo {
	var info HostInfo

	hostInfo, err := host.Info()
	if err != nil {
		return info
	}

	info = HostInfo{
		Hostname:             hostInfo.Hostname,
		Uptime:               hostInfo.Uptime,
		UptimeHuman:          humanize.Time(time.Now().Add(-time.Second * time.Duration(hostInfo.Uptime))),
		BootTime:             hostInfo.BootTime,
		BootTimeHuman:        time.Unix(int64(hostInfo.BootTime), 0).Format("2006-01-02 15:04:05"),
		Procs:                hostInfo.Procs,
		OS:                   hostInfo.OS,
		Platform:             hostInfo.Platform,
		PlatformFamily:       hostInfo.PlatformFamily,
		PlatformVersion:      hostInfo.PlatformVersion,
		KernelVersion:        hostInfo.KernelVersion,
		KernelArch:           hostInfo.KernelArch,
		VirtualizationSystem: hostInfo.VirtualizationSystem,
		VirtualizationRole:   hostInfo.VirtualizationRole,
		HostID:               hostInfo.HostID,
	}

	return info
}
